package com.yc.util;

import cn.hutool.core.util.CharsetUtil;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;

import java.io.File;
import java.util.List;

/**
 * <p>
 * Zip工具类
 * <p>
 *
 * @author BAO
 * @since 2020/12/6 17:02
 */
public class ZipUtil {
    public static boolean unZip(String zipPath, String destinationPath) {
        boolean isOk = true;
        ZipFile zipFile = new ZipFile(zipPath);
        try {
            zipFile.setCharset(CharsetUtil.systemCharset());
            zipFile.extractAll(destinationPath);
        } catch (ZipException e) {
            e.printStackTrace();
            isOk = false;
        } finally {
            return isOk;
        }
    }
    public static void zip(List<File> files, File outZip) {
        // 生成的压缩文件
        ZipFile zipFile = new ZipFile(outZip);
        ZipParameters parameters = new ZipParameters();
        // 压缩方式
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        // 压缩级别
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        try {
            zipFile.addFiles(files);
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
}
